<?php
/**
 * Created by PhpStorm.
 * User: mww
 * 好友
 */
namespace app\api\controller;
use think\Db;
class Friend extends Base {

    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }

    /**
     * 好友首页
     * user_hospital：医院或企业名称
     * user_room：科室或团队名字
     * team_num：数量
     * @return \think\response\Json
     */
    public function index(){
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_hospital,user_room',2);
        $room_num = Db::table('up_u_user')
            ->where('user_hospital',$user['user_hospital'])
            ->where('user_room',$user['user_room'])
            ->where('user_status',1)
            ->count();
        $user['team_num'] = $room_num;
        $user['user_hospital_name'] = $user['user_hospital'];
        $user['user_room_name'] = $user['user_room'];
        if($user['user_type'] != 1){
            $newFid = Db::table('up_company_team')->where('ct_id',$user['user_room'])->value('ct_fid_str');
            $newCtid = Db::table('up_company_team')
                ->where('ct_fid_str','like',"%$newFid%")
                ->where('ct_status',1)
                ->column('ct_id');
            $room_num = Db::table('up_u_user')
                ->where('user_hospital',$user['user_hospital'])
                ->where('user_room','in',$newCtid)
                ->where('user_status',1)
                ->count();
            $user['team_num'] = $room_num;
            $company = Db::table('up_company')->where('c_id',$user['user_hospital'])->value('c_name');
            $room = Db::table('up_company_team')->where('ct_id',$user['user_room'])->value('ct_name');
            $user['user_hospital_name'] = $company;
            $user['user_room_name'] = $room;
        }
        return  json(array('status'=>200,'msg'=>$user));
    }

    /**
     * 组织架构
     * @return \think\response\Json
     */
    public function myTeams(){
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_hospital',2);
        $data = Db::table('up_u_user')
            ->where('user_hospital',$user['user_hospital'])
            ->where('user_status',1)
            ->group('user_room')
            ->field('user_room,user_room user_room_name,count(*) num')
            ->page($page,20)
            ->select();
        if($user['user_type'] != 1){
            $data = Db::table('up_company_team')
                ->where('ct_c_id',$user['user_hospital'])
                ->where('ct_fid',0)
                ->where('ct_status',1)
                ->field('ct_id user_room,ct_name user_room_name,ct_fid_str')
                ->page($page,20)
                ->select();
            if($data){
                foreach ($data as $k => $v){
                    $newCtid = Db::table('up_company_team')
                        ->where('ct_fid_str','like',"%{$v['ct_fid_str']}%")
                        ->where('ct_status',1)
                        ->column('ct_id');
                    $room_num = Db::table('up_u_user')
                        ->where('user_hospital',$user['user_hospital'])
                        ->where('user_room','in',$newCtid)
                        ->where('user_status',1)
                        ->count();
                    $data[$k]['num'] = $room_num;
                }
            }
        }
        return  json(array('status'=>200,'msg'=>$data));
    }
    /**
     * 组织架构内的成员
     * @return \think\response\Json
     */
    public function myTeamUsers(){
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_hospital',2);
        $list = Db::table('up_u_user')
            ->where('user_hospital',$user['user_hospital'])
            ->where('user_room',$user_room)
            ->where('user_status',1)
            ->field('user_id,user_username,user_image,user_room,user_job_desc')
            ->select();
        $teams = [];
        if($user['user_type'] != 1){
            foreach ($list as $kk => $vv){
                $room = Db::table('up_company_team')->where('ct_id',$vv['user_room'])->value('ct_name');
                $list[$kk]['user_room'] = $room;
            }
            //
            $teams = Db::table('up_company_team')
                ->where('ct_fid',$user_room)
                ->where('ct_status',1)
                ->field('ct_id user_room,ct_name user_room_name,ct_fid_str')
                ->select();
            if($teams){
                foreach ($teams as $kk2 => $vv2){
                    $newCtid = Db::table('up_company_team')
                        ->where('ct_fid_str','like',"%{$vv2['ct_fid_str']}%")
                        ->where('ct_status',1)
                        ->column('ct_id');
                    $room_num = Db::table('up_u_user')
                        ->where('user_hospital',$user['user_hospital'])
                        ->where('user_room','in',$newCtid)
                        ->where('user_status',1)
                        ->count();
                    $teams[$kk2]['num'] = $room_num;
                }
            }
        }
        return  json(array('status'=>200,'msg'=>$list,'teams'=>$teams));
    }
    /**
     * other_uid
     * 添加好友（每一次进去聊之前都要调这个接口）
     * @return \think\response\Json
     */
    public function addFriend(){
        extract(input());
        if($user_id == $other_uid){
            return  json(['status'=>200,'msg'=>"不能自己添加自己为好友"]);
        }else{
            $today = date('Y-m-d H:i:s');
            $where = [
                'friend_user_id' => $user_id,
                'friend_other_user' => $other_uid
            ];
            $where2 = [
                'friend_user_id' => $other_uid,
                'friend_other_user' => $user_id
            ];
            $have = Db::table('up_friend')->where($where)->find();
            $have2 = Db::table('up_friend')->where($where2)->find();
            $data = [];
            if(!$have){
                $data[] = [
                    'friend_user_id' => $user_id,
                    'friend_other_user' => $other_uid,
                    'friend_time' => $today,
                ];
            }
            if(!$have2){
                $data[] = [
                    'friend_user_id' => $other_uid,
                    'friend_other_user' => $user_id,
                    'friend_time' => $today,
                ];
            }
            if($data){
               $res = Db::table('up_friend')->insertAll($data);
                if($res){
                    //入互动记录(以天为单位)
                    if(isset($type) && $type){
                        $i_h = [
                            'type' => $type,
                            'p_id' => $p_id,
                            'date' => date('Y-m-d')
                        ];
                        $have = Db::table('up_zz_friend')
                            ->where($i_h)
                            ->find();
                        if($have){
                            Db::table('up_zz_friend')->where('id',$have['id'])->setInc('times');
                        }else{
                            if($type == 1){
                                $c_id = Db::table('up_company_product')->where('cp_id',$p_id)->value('cp_c_id');
                            }else{
                                $c_id = Db::table('up_project')->where('pro_id',$p_id)->value('pro_c_id');
                            }
                            $i_h['c_id'] = $c_id;
                            Db::table('up_zz_friend')->insert($i_h);
                        }
                    }

                    return json(['status'=>200,'msg'=>'操作成功']);
                }else{
                    return json(['status'=>100,'msg'=>'操作失败，请刷新重试']);
                }
            }else{
                return json(['status'=>200,'msg'=>'已经是好友']);
            }
        }
    }

    /**
     * 好友通讯录
     * @return \think\response\Json
     */
    public function friendList(){
        extract(input());
        $group = Db::table('up_friend')->alias('f')
            ->join('up_u_user u','u.user_id = f.friend_other_user','left')
            ->where('friend_user_id',$user_id)
            ->where('user_status',1)
            ->group('user_hospital')
            ->field('user_hospital,user_type')
            ->page($page,20)
            ->select();
        $data = [];
        if($group){
            foreach ($group as $k=>$v){
                if($v['user_type'] != 1){
                    $company = Db::table('up_company')->where('c_id',$v['user_hospital'])->value('c_name');
                    $data[$k]['user_hospital_name'] = $company;
                }else{
                    $data[$k]['user_hospital_name'] = $v['user_hospital'];
                }
                $where = array();
                $where['friend_user_id'] = $user_id;
                $where['user_hospital'] = $v['user_hospital'];
                $list = Db::table('up_friend')->alias('f')
                    ->join('up_u_user u','u.user_id = f.friend_other_user','left')
                    ->field('friend_id,friend_type,user_id,user_username,user_image,user_hospital,user_room,user_job_desc,user_type')
                    ->order('friend_time desc')
                    ->where($where)
                    ->where('user_status',1)
                    ->select();
                if($list){
                    foreach ($list as $kk => $vv){
                        if($vv['user_type'] != 1){
                            $room = Db::table('up_company_team')->where('ct_id',$vv['user_room'])->value('ct_name');
                            $list[$kk]['user_room'] = $room;
                        }
                    }
                    $data[$k]['all'] = $list;
                }else{
                    $data[$k]['all'] = [];
                }
            }
        }
        //我的企业名字或者医院名字是什么
        $user = $this->getUserInfo($user_id,'user_type,user_hospital',2);
        $my_msg = $user['user_hospital'];
        if($user['user_type'] == 2){
            $my_msg = Db::table('up_company')->where('c_id',$user['user_hospital'])->value('c_name');
        }
        return  json(array('status'=>200,'msg'=>$data,'my_msg'=>$my_msg));

    }
    /**
     * page
     * 关键好友：
     * @return \think\response\Json
     */
    public function keyFriendList(){
        extract(input());
        $list = Db::table('up_friend')->alias('f')
            ->join('up_u_user u','u.user_id = f.friend_other_user','left')
            ->where('friend_user_id',$user_id)
            ->where('friend_type',2)
            ->where('user_status',1)
            ->field('friend_id,friend_type,user_id,user_username,user_image,user_room user_room_name,user_job_desc,user_type,user_hospital user_hospital_name')
            ->page($page,20)
            ->select();
        if($list){
            foreach ($list as $kk => $vv){
                if($vv['user_type'] != 1){
                    $company = Db::table('up_company')->where('c_id',$vv['user_hospital_name'])->value('c_name');
                    $room = Db::table('up_company_team')->where('ct_id',$vv['user_room_name'])->value('ct_name');
                    $list[$kk]['user_hospital_name'] = $company;
                    $list[$kk]['user_room_name'] = $room;
                }
            }
        }
        return  json(array('status'=>200,'msg'=>$list));

    }
    /**
     * page
     * 设置或取消关键好友：
     * @return \think\response\Json
     */
    public function friendKeySet(){
        extract(input());
        $list = Db::table('up_friend')
            ->where('friend_id',$friend_id)
            ->find();
        $update = [
            'friend_time' => date('Y-m-d H:i:s'),
            'friend_type' => 1
        ];
        if($list['friend_type'] == 1){
            $update['friend_type'] = 2;
        }
        $res = Db::table('up_friend')
            ->where('friend_id',$friend_id)
            ->update($update);
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请刷新重试']);
        }

    }
    /**
     * 搜索
     * @return \think\response\Json
     */
    public function friendSearch(){
        extract(input());
        $where = [
            'user_status' => 1,
            'user_verify' => 2
        ];
        if(isset($keyword) && $keyword){
            $where['user_username|user_hospital|user_room|c_name|ct_name'] = ['like',"%$keyword%"];
        }
        $friend = Db::table('up_friend')->alias('f')
            ->join('up_u_user u','u.user_id = f.friend_other_user','left')
            ->join('up_company c','u.user_hospital = c.c_id','left')
            ->join('up_company_team ct','u.user_room = ct.ct_id','left')
            ->where('friend_user_id',$user_id)
            ->where($where)
            ->field('user_id,user_type,user_username,user_image,user_hospital,user_room,c_name,ct_name')
            ->select();
        $uids = array_column ($friend,'user_id');
        $uids[] = $user_id;
        $friend_no = Db::table('up_u_user')->alias('u')
            ->join('up_company c','u.user_hospital = c.c_id','left')
            ->join('up_company_team ct','u.user_room = ct.ct_id','left')
            ->where('user_id','not in',$uids)
            ->where($where)
            ->field('user_id,user_type,user_username,user_image,user_hospital,user_room,c_name,ct_name')
            ->select();

        return  json(array('status'=>200,'msg'=>$friend,'msg_no'=>$friend_no));

    }

}